package com.nowcoder.community.controller;

import com.nowcoder.community.entity.Event;
import com.nowcoder.community.event.EventProducer;
import com.nowcoder.community.util.CommunityConstant;
import com.nowcoder.community.util.CommunityUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;

/**
 * @author cjh
 * @date 2023/2/13 - 11:03
 */
@Controller
@Slf4j
@RequiredArgsConstructor
public class ShareController implements CommunityConstant {
    private final EventProducer eventProducer;

    @Value("${community.path.domain}")
    private String domain;
    @Value("${server.servlet.context-path}")
    private String contextPath;

    @Value("${wk.image.storage}")
    private String wkImageStorage;
    @Value("${qiniu.bucket.share.url}")
    private String shareBucketUrl;

    @RequestMapping(path = "/share", method = RequestMethod.GET)
    @ResponseBody
    public String share(String htmlUrl) {
//        文件名
        String fileName = CommunityUtil.generateUUID();
//        异步的生成 长图
        Event event = new Event()
                .setTopic(TOPIC_SHARE)
                .setData("htmlUrl", htmlUrl)
                .setData("fileName", fileName)
                .setData("suffix", ".png");
        eventProducer.fireEvent(event);

        HashMap<String, Object> map = new HashMap<>();
//        web路径
//        map.put("shareUrl", domain + contextPath + "/share/image/" + fileName);
//       七牛云的路径
        map.put("shareUrl",shareBucketUrl+"/"+fileName);
//         返回访问路径
        return CommunityUtil.getJsonString(0, null, map);
    }


    /**
     * 废弃
     * 获取长图
     * @param fileName
     * @param response
     */
    @RequestMapping(path = "/share/image/{fileName}", method = RequestMethod.GET)
    public void getShareImage(@PathVariable("fileName") String fileName, HttpServletResponse response) {
        if (StringUtils.isBlank(fileName)) {
            throw new IllegalArgumentException("文件名不能为空 ");
        }

        response.setContentType("image/png");
        File file = new File(wkImageStorage + "/" + fileName + ".png");
        try (OutputStream os = response.getOutputStream();
             FileInputStream fis = new FileInputStream(file);
        ) {
//            缓冲
            byte[] buffer = new byte[1024];
//            游标
            int b = 0;
            while ((b = fis.read(buffer)) != -1) {
                os.write(buffer, 0, b);
            }

        } catch (IOException e) {
            log.error("文件读取失败", e);
        }
    }


}
